<!DOCTYPE html>
<!--
Copyright (c) 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/base/unit.html">
<link rel="import" href="/tracing/ui/base/line_chart.html">

<script>
'use strict';
tr.b.unittest.testSuite(function() {
  test('instantiation_singleSeries', function() {
    const chart = new tr.ui.b.LineChart();
    chart.displayXInHover = true;
    this.addHTMLOutput(chart);
    chart.data = [
      {x: 10, y: 100},
      {x: 20, y: 110},
      {x: 30, y: 100},
      {x: 40, y: 50}
    ];
  });

  test('instantiation_twoSeries', function() {
    const chart = new tr.ui.b.LineChart();
    chart.displayXInHover = true;
    this.addHTMLOutput(chart);
    chart.data = [
      {x: 10, alpha: 100, beta: 50},
      {x: 20, alpha: 110, beta: 75},
      {x: 30, alpha: 100, beta: 125},
      {x: 40, alpha: 50, beta: 125}
    ];

    const r = new tr.b.math.Range();
    r.addValue(20);
    r.addValue(40);
    chart.brushedRange = r;
  });

  test('instantiation_twoSparseSeriesWithFirstValueSparse', function() {
    const chart = new tr.ui.b.LineChart();
    chart.displayXInHover = true;
    this.addHTMLOutput(chart);
    chart.data = [
      {x: 10, alpha: 20, beta: undefined},
      {x: 20, alpha: undefined, beta: 10},
      {x: 30, alpha: 10, beta: undefined},
      {x: 45, alpha: undefined, beta: 20},
      {x: 50, alpha: 30, beta: 30}
    ];
  });

  test('instantiation_twoSparseSeriesWithFirstValueNotSparse', function() {
    const chart = new tr.ui.b.LineChart();
    chart.displayXInHover = true;
    this.addHTMLOutput(chart);
    chart.data = [
      {x: 10, alpha: 20, beta: 40},
      {x: 20, alpha: undefined, beta: 10},
      {x: 30, alpha: 10, beta: undefined},
      {x: 45, alpha: undefined, beta: 20},
      {x: 50, alpha: 30, beta: undefined}
    ];
  });

  test('brushRangeFromIndices', function() {
    const chart = new tr.ui.b.LineChart();
    chart.displayXInHover = true;
    this.addHTMLOutput(chart);
    chart.data = [
      {x: 10, value: 50},
      {x: 30, value: 60},
      {x: 70, value: 70},
      {x: 80, value: 80},
      {x: 120, value: 90}
    ];
    let r = new tr.b.math.Range();

    // Range min should be 10.
    r = chart.computeBrushRangeFromIndices(-2, 1);
    assert.strictEqual(r.min, 10);

    // Range max should be 120.
    r = chart.computeBrushRangeFromIndices(3, 10);
    assert.strictEqual(r.max, 120);

    // Range should be [10, 120]
    r = chart.computeBrushRangeFromIndices(-2, 10);
    assert.strictEqual(r.min, 10);
    assert.strictEqual(r.max, 120);

    // Range should be [20, 100]
    r = chart.computeBrushRangeFromIndices(1, 3);
    assert.strictEqual(r.min, 20);
    assert.strictEqual(r.max, 100);
  });

  test('instantiation_interactiveBrushing', function() {
    const chart = new tr.ui.b.LineChart();
    chart.displayXInHover = true;
    this.addHTMLOutput(chart);
    chart.data = [
      {x: 10, value: 50},
      {x: 20, value: 60},
      {x: 30, value: 80},
      {x: 40, value: 20},
      {x: 50, value: 30},
      {x: 60, value: 20},
      {x: 70, value: 15},
      {x: 80, value: 20}
    ];

    let mouseDownIndex = undefined;
    let curMouseIndex = undefined;

    function updateBrushedRange() {
      if (mouseDownIndex === undefined) {
        chart.brushedRange = new tr.b.math.Range();
        return;
      }
      chart.brushedRange = chart.computeBrushRangeFromIndices(
          mouseDownIndex, curMouseIndex);
    }

    chart.addEventListener('item-mousedown', function(e) {
      mouseDownIndex = e.index;
      curMouseIndex = e.index;
      updateBrushedRange();
    });
    chart.addEventListener('item-mousemove', function(e) {
      if (e.button === undefined) return;
      curMouseIndex = e.index;
      updateBrushedRange();
    });
    chart.addEventListener('item-mouseup', function(e) {
      curMouseIndex = e.index;
      updateBrushedRange();
    });
  });

  test('overrideDataRange', function() {
    let chart = new tr.ui.b.LineChart();
    chart.displayXInHover = true;
    this.addHTMLOutput(chart);
    chart.overrideDataRange = tr.b.math.Range.fromExplicitRange(10, 90);
    chart.data = [
      {x: 0, value: 0},
      {x: 1, value: 100},
    ];

    chart = new tr.ui.b.LineChart();
    chart.displayXInHover = true;
    this.addHTMLOutput(chart);
    chart.overrideDataRange = tr.b.math.Range.fromExplicitRange(-10, 100);
    chart.data = [
      {x: 0, value: 0},
      {x: 1, value: 50},
    ];
  });

  test('sizeInBytes', function() {
    const chart = new tr.ui.b.LineChart();
    chart.unit = tr.b.Unit.byName.sizeInBytes;
    chart.yLogScaleBase = 2;
    chart.graphHeight = 400;
    chart.isYLogScale = true;
    chart.hideLegend = true;
    this.addHTMLOutput(chart);
    chart.data = [
      {x: 0, value: 1},
      {x: 1, value: 1 << 10},
      {x: 2, value: 1 << 20},
      {x: 3, value: 1 << 30},
    ];
  });
});
</script>
